<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //调用格式不同     sum([1,2,3])   sum(1,2,3)
        //如果没有参数     sum([])        sum()
        function sun(nums) {
            var total = 0;
            for (var i = 0; i < nums.length; i++) {
                total += nums[i];
            }
            return total;
        }
        //用数组做参数，调用函数，没有数据时，必须要有一个空数组
        console.log(sum([]));
        // console.log(sum());  //会报错


        function sun(...nums) {
            var total = 0;
            for (var i = 0; i < nums.length; i++) {
                total += nums[i];
            }
            return total;
        }
        //剩余参数做形参，调用函数，没有数据时，可省略中括号
        console.log(sum());

    </script>
</body>

</html>